﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using DreamBox.Net.Data;
using System.Web.Script.Serialization;

namespace DreamBox.Net.Core
{
    public static class Json
    {
        /// <summary>
        /// JSON转 DbTable
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static DbTable ToDbTable(string json)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            List<Dictionary<string, object>> list = serializer.Deserialize<List<Dictionary<string, object>>>(json);

            DbTable dt = null;
            if (list.Count > 0)
            {
                dt = new DbTable();

                foreach (Dictionary<string, object> d in list)
                {
                    DbRow row = new DbRow();
                    
                    foreach (string key in d.Keys)
                    {
                        DbColumn dc = new DbColumn();
                        dc.Name = key;
                        dc.Value = d[key];
                        row.Add(dc);
                    }
                    dt.Rows.Add(row);
                }
            }
            return dt;
        }
        /// <summary>
        /// JSON转 DataTable
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static DataTable ToDataTable(string json)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            List<Dictionary<string, object>> list = serializer.Deserialize<List<Dictionary<string,object>>>(json);

            DataTable dt = null;
            if (list.Count > 0)
            {
                dt = new DataTable();

                foreach (Dictionary<string, object> d in list)
                {
                    if (dt.Columns.Count == 0)
                    {
                        //创建列
                        foreach (string key in d.Keys)
                        {
                            dt.Columns.Add(key, d[key].GetType());
                        }
                    }
                    //创建行并填充数据
                    DataRow row = dt.NewRow();
                    foreach (string key in d.Keys)
                    {
                        row[key] = d[key];
                    }
                    dt.Rows.Add(row);
                }
            }
            return dt;
        }
        /// <summary>
        /// Json转DataTable 带jsonType标识的
        /// </summary>
        /// <param name="json"></param>
        /// <param name="jsonType"></param>
        /// <returns></returns>
        public static DataTable ToDataTable(string json,string jsonType)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            DataTable dt = null;
            
            //处理带jsonType序列化的JSON数据
            List<Dictionary<string, object>> jsonList = serializer.Deserialize<List<Dictionary<string, object>>>("["+json+"]");
            if (jsonList.Count > 0)
            {
                string jsonData=null;
                foreach (string key in jsonList[0].Keys)
                {
                    jsonData = serializer.Serialize(jsonList[0][key]);
                }
                List<Dictionary<string, object>> list = serializer.Deserialize<List<Dictionary<string, object>>>(jsonData);
                if (list.Count > 0)
                {
                    dt = new DataTable();
                    dt.TableName = jsonType;
                    foreach (Dictionary<string, object> d in list)
                    {
                        if (dt.Columns.Count == 0)
                        {
                            //创建列
                            foreach (string key in d.Keys)
                            {
                                dt.Columns.Add(key, d[key].GetType());
                            }
                        }
                        //创建行并填充数据
                        DataRow row = dt.NewRow();
                        foreach (string key in d.Keys)
                        {
                            row[key] = d[key];
                        }
                        dt.Rows.Add(row);
                    }
                }
            }
            
            return dt;
        }
        /// <summary>
        /// DataTable转JSON 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string ToJson(DataTable dt)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            //用来存储行数据
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

            foreach (DataRow dr in dt.Rows)
            {
                Dictionary<string, object> d = new Dictionary<string, object>();
                foreach (DataColumn dc in dt.Columns)
                {
                    d.Add(dc.ColumnName, dr[dc]);
                }
                list.Add(d);
            }
            return serializer.Serialize(list);
        }
        /// <summary>
        /// 根据不同类型返回JSON格式
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="jsonType"></param>
        /// <returns></returns>
        public static string ToJson(DataTable dt,string jsonType)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            //用来存储行数据
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

            foreach (DataRow dr in dt.Rows)
            {
                Dictionary<string, object> d = new Dictionary<string, object>();
                foreach (DataColumn dc in dt.Columns)
                {
                    d.Add(dc.ColumnName, dr[dc]);
                }
                list.Add(d);
            }
            return "{\""+jsonType+"\":"+serializer.Serialize(list)+"}";
        }
        /// <summary>
        /// DbTable 转Json
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public static string ToJson(DbTable dt)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            //用来存储行数据
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

            foreach (DbRow dr in dt.Rows)
            {
                Dictionary<string, object> d = new Dictionary<string, object>();
                foreach (DbColumn dc in dr.Columns)
                {
                    d.Add(dc.Name, dc.Value);
                }
                list.Add(d);
            }
            return serializer.Serialize(list);
        }
        
    }
}
